﻿using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using VIT.DataHelper.DataAccess;

namespace VIT.Business
{
    public class Article : Article_Base
    {
        SQLSupport sqlSupport = new SQLSupport();

        #region Constructor
        public Article(string id = "",
                        string categoryId = "",
                        string languageId = "",
                        string image = "",
                        string title = "",
                        string brief = "",
                        string contents = "",
                        string tag = "")
        {
            this.Id = id;
            this.CategoryId = categoryId;
            this.LanguageId = languageId;
            this.Image = image;
            this.Title = title;
            this.Brief = brief;
            this.Contents = contents;
            this.Tag = tag;
            this.DisplayDate = DateTime.Now;
        }
        #endregion

        #region FrontEnd
        public DataTable GetList(string categoryIds, int start=0, int end=0)
        {
            string strQuery = "";
            if (start == 0 && end == 0) strQuery = "select Id, Image, Title, Brief, Tag, DisplayDate FROM Article, Article_Language l where Id=ArticleId and CategoryId in ("
                                + categoryIds + ") and LanguageId=@LanguageId and Published=1 order by Orders, DisplayDate DESC";
            else strQuery = "select Id, Image, Title, Brief, Tag, DisplayDate" +
                             " from (SELECT  ROW_NUMBER() OVER (ORDER BY Orders, l.ModifyDate DESC)" +
                             " as Row, Id, Image, Title, Brief, Tag, DisplayDate FROM Article, Article_Language l where Id=ArticleId and CategoryId in ("
                             + categoryIds + ") and LanguageId=@LanguageId and Published=1) as Filters" +
                             " WHERE  Row >= @Start AND Row <= @End";
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            SqlParameter Start = new SqlParameter("@Start", start);
            SqlParameter End = new SqlParameter("@End", end);
            return sqlSupport.ExecuteDataset(false, strQuery, LanguageId, Start, End).Tables[0];
        }        

        public void getSingle()
        {
            string strQuery = "select CategoryId, Title, Image, Brief, Contents, Tag, DisplayDate" +
                                " from Article, Article_Language where Id=ArticleId and LanguageID=@LanguageId and Id=@Id and Published=1";
            SqlParameter Id = new SqlParameter("@Id", this.Id);
            SqlParameter LanguageId = new SqlParameter("@LanguageId", this.LanguageId);
            DataTable dt = sqlSupport.ExecuteDataset(false, strQuery, Id, LanguageId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                this.Image = dt.Rows[0]["Image"].ToString();
                this.Title = dt.Rows[0]["Title"].ToString();
                this.Brief = dt.Rows[0]["Brief"].ToString();
                this.Contents = dt.Rows[0]["Contents"].ToString();
                this.Tag = dt.Rows[0]["Tag"].ToString();
                this.CategoryId = dt.Rows[0]["CategoryId"].ToString();
                if (!string.IsNullOrEmpty(dt.Rows[0]["DisplayDate"].ToString()))
                    this.DisplayDate = (DateTime)dt.Rows[0]["DisplayDate"];
            }
            else this.Id = "";
        }
        #endregion
    }
}
